<?php


namespace App\Api\Controllers;


use App\Models\Cart;
use App\Models\Product;
use App\Models\ProductSku;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class CartController extends  Controller
{
    //加入购物车
    public function add(Request $request)
    {
        //??加商品规格判断，不存在，不能加入购物车
        $input = $request->only([
            'product_id',
            'product_sku_id',
            'amount',
        ]);
        $validator = Validator::make($input,[
            'product_id' => 'required',
            'product_sku_id' => 'required',
            'amount' => 'required',
        ]);

        if ($validator->fails()) {
            return $this->responseFailed($validator->errors()->first());
        }
        //判断商品是否意见下架
        $user = auth()->user();
        $input['user_id'] = $user['id'];
        $where = [
            ['product_id' ,'=',$input['product_id']],
            ['product_sku_id' ,'=',$input['product_sku_id']],
            ['user_id' ,'=',$input['user_id']],
        ];
        $product_sku = ProductSku::getSkuById($input['product_sku_id']);
        if (!$product_sku) {
            return $this->responseFailed('此规格不存在');
        }
        if ($user['is_proxy'] == 1) {
            if ($product_sku['vip_price'] < 1100) {
                return $this->responseFailed('代理请购买箱装');
            }
        }
        $product = Product::find($input['product_id']);
        if (!$product) {
            return $this->responseFailed('无此商品');
        }
        if($product['on_sale'] != 1){
            return $this->responseFailed('商品已下架');
        }
        $cart = Cart::where($where)->first();
        if (!$cart) {
            $cart = new Cart();
            $cart->fill($input);
        }
        $amount = $input['amount'];
        if ($amount <= 0) {
            return $this->responseFailed('购物数小于1');
        }
        //判断是否有库存
        $sku = ProductSku::find($input['product_sku_id']);
        if (!$sku) {
            return $this->responseFailed('无此商品');
        }
        if ($sku['stock'] < $amount) {
            return $this->responseFailed('库存不足');
        }
        $cart->amount = $amount;
        if(!$cart->save()){
            return $this->responseFailed();
        }
        return $this->responseData($cart->toArray());
    }

    //购物车列表
    public function index()
    {
        $user = auth()->user();
        $data = Cart::where('user_id',$user['id'])
            ->orderBy('id','desc')
            ->paginate(10)
            ->toArray();
        foreach($data['data'] as &$val){
            $sku = ProductSku::find($val['product_sku_id']);
            if ($sku) {
                $sku = $sku->toArray();
            }
            $val['sku'] = $sku;
            $val['product'] = Product::getProduct($val['product_id']);
            $val['bool'] = false;
        }
        return $this->responseData($data);
    }

    //删除购物车
    public function del(Request $request)
    {
        if (!$request->cart_id) {
            return $this->responseFailed('cart_id 必传');
        }
        $cart = Cart::find($request->cart_id);
        if ($cart && $cart->delete()) {
            return $this->responseSuccess('删除成功');
        }
        return $this->responseFailed('已删除');
    }
}